package cn.lzit.request.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
@WebServlet(name = "ResultServlet", urlPatterns = {"/result"})  // 确保这里的路径为 "/result"
public class ResultServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pWriter = response.getWriter();
        pWriter.print("<body style='background-color:pink;'>");

        // 获取请求参数
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");
        String age = request.getParameter("age");
        String sex = request.getParameter("sex");
        String[] loves = request.getParameterValues("loves");
        String email = request.getParameter("email");
        String tel = request.getParameter("tel");

        // 输入检查
        if (name == null || name.isEmpty()) {
            pWriter.print("<h1>用户名不能为空</h1>");
            pWriter.print("</body>");
            return;
        }
        // 密码检查
        if (pwd == null || pwd.length() < 6 || pwd.length() > 10) {
            pWriter.print("<h1>密码长度必须在6到10位之间</h1>");
            pWriter.print("</body>");
            return;
        }
        // 年龄检查
        if (age == null || age.isEmpty()) {
            pWriter.print("<h1>年龄不能为空</h1>");
            pWriter.print("</body>");
            return;
        }
        // 性别检查
        if (sex == null || sex.isEmpty()) {
            pWriter.print("<h1>性别不能为空</h1>");
            pWriter.print("</body>");
            return;
        }

        // 输出用户信息
        pWriter.print("用户名：" + name + "<br>");
        pWriter.print("密码：" + pwd + "<br>");
        pWriter.print("年龄：" + age + "<br>");
        pWriter.print("性别：" + sex + "<br>");
        pWriter.print("爱好: " + (loves != null ? Arrays.toString(loves) : "无") + "<br/>");
        pWriter.print("Email：" + (email != null ? email : "无") + "<br>");
        pWriter.print("Tel：" + (tel != null ? tel : "无") + "<br>");
        pWriter.print("</body>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
